package es.usal.proyecto.Controller.Tasks;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import es.usal.proyecto.Controller.Interfaces.IAsyncConnectivity;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;

public class ConnectivityTask extends AsyncTask<String, Integer, Boolean>{

	private static final String LOG_TAG = null;
	public IAsyncConnectivity delegate=null;
	private Context context;
	
	public ConnectivityTask(Context context) {
		this.context = context;
	}
	
	@Override
	protected Boolean doInBackground(String... params) {
		// TODO Auto-generated method stub
		ConnectivityManager mgr 
        = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	   
		NetworkInfo network = mgr.getActiveNetworkInfo();
		if(network == null)
	   	return false;
	   
		try {
			HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
			urlc.setRequestProperty("User-Agent", "Test");
			urlc.setRequestProperty("Connection", "close");
			urlc.setConnectTimeout(1500); 
			urlc.connect();
			return (urlc.getResponseCode() == 200);
		} catch (IOException e) {
			Log.e(LOG_TAG, "Error checking internet connection", e);
			return false;
		}
	}
	

	@Override
    protected void onPostExecute(Boolean result) {
		delegate.processFinish(result);
    }
}
